﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using E_Commerce.Domain.Interfaces;
using Dapper;
using Microsoft.Extensions.Configuration;
using E_Commerce.Domain.Entities;
using E_Commerce.Application.Dto;//引用
using E_Commerce.Domain.Dto;

namespace E_Commerce.Infrastructure.Data
{
   public class OrderRepository : DapperRepository<Orders>,IOrderRepository
    {
        /// <summary>
        /// 依赖注入
        /// </summary>
        private IConfiguration _configuration;
        public OrderRepository(IConfiguration configuration)
        {
            this._configuration = configuration;
            this.ConnStr = _configuration.GetConnectionString("ConnStr");
            SimpleCRUD.SetDialect(SimpleCRUD.Dialect.MySQL);
            Conn = new MySql.Data.MySqlClient.MySqlConnection(this.ConnStr);
        }
        /// <summary>
        /// 生成预支付订单
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public async Task<int> Advancepayment(Orders o)
        {
            return (int)await Conn.InsertAsync(o);
        }

        /// <summary>
        /// 删除订单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleOrderAsync(int id)
        {
            try
            {
                string sql = $"delete from Orders where OrderId={id}";
                return await Conn.DeleteAsync(sql);
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            
        }
        /// <summary>
        /// 反填订单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Orders> FillOrderAsync(int id)
        {
            return await Conn.GetAsync<Orders>(id);
        }
        /// <summary>
        /// 显示+查询+分页订单
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="oName"></param>
        /// <param name="orderstatic"></param>
        /// <returns></returns>
        public List<OrdersDto> GetOrders(int pageIndex, int pageSize, out int totalCount, string OrderNumber, int orderstatic)
        {
            string sql = $"select * from users a left join orders b on a.UserId=b.UserId left join Product d on b.ProductId=d.ProId where 1=1";
            if (!string.IsNullOrEmpty(OrderNumber))
            {
                sql += $" and o.OrderNumber like '%{OrderNumber}%'";
            }
            sql += $"  limit @a1,@a2";
            var sqlcount = "select count(1) from Orders";
            totalCount = Conn.ExecuteScalar<int>(sqlcount);
            return Conn.Query<OrdersDto>(sql, new { a1 = (pageIndex - 1) * pageSize, a2 = pageSize }).ToList();

        }
        /// <summary>
        /// 订单详情页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<List<ProductDto>> OrderFind(int id)
        {
            string sql = $"select * from Product p right join OrderDetail d on p.ProId=d.PrIds where OrId={id}";
            return (List<ProductDto>)await Conn.QueryAsync<ProductDto>(sql);
        }

        /// <summary>
        /// 修改订单
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public async Task<int> UptOrderAsync(Orders o)
        {
            try
            {
                return await Conn.UpdateAsync(o);
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            
        }

        /// <summary>
        /// 修改订单状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int UptOrderStatic(int id)
        {
            string sql = $"update Orders set OrderStatus=1 where OrderId={id}";
            return Conn.Execute(sql);
        }
    }
}
